/**    
 * 文件名：ApprovalRest.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年10月10日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.models.approval;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.ApprovalLogRecord;
import com.logic.landseaserver.models.approval.bean.ApprovalLogReq;
import com.logic.landseaserver.models.approval.bean.ApprovalReq;
import com.logic.landseaserver.models.approval.bean.GetApprovalFlowReq;
import com.logic.landseaserver.models.approval.bean.GetApprovalFlowResp;
import com.logic.landseaserver.models.approval.bean.SubmitApprovalReq;
import com.logic.landseaserver.models.approval.service.ApprovalService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：ApprovalRest</br>
 * 类描述：审批</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年10月10日 下午4:39:20</br>
 * @version 1.0
 *
 */
@Path("/approval")
@Api(value = "审批")
public class ApprovalRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ApprovalRest.class);
    
    @Autowired
    private ApprovalService approvalService;
    
    /**
     * 
     * [简要描述]：获取审核流程</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-10-12)</br>
     *
     * @param request
     * @return
     *
     */
    @POST
    @Path("/getApprovalFlow")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据当前操作人及场景获取可选流程列表")
    public Response getApprovalFlow(@ApiParam(value = "param", required = true) GetApprovalFlowReq request)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            GetApprovalFlowResp response = approvalService.getApprovalFlow(request.getTaskType());
            map.setData(response.getResultList());
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getApprovalFlow", e, map);
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    /**
     * 
     * [简要描述]：提交审核</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-10-13)</br>
     *
     * @param submitApprovalReq
     * @return
     *
     */
    @POST
    @Path("/submitApproval")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "提交审核")
    public Response submitApproval(@ApiParam(value = "param", required = true) SubmitApprovalReq submitApprovalReq)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            approvalService.submitApproval(submitApprovalReq);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "submitApproval", e, map);
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    /**
     * 
     * [简要描述]：审核</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-10-13)</br>
     *
     * @param submitApprovalReq
     * @return
     *
     */
    @POST
    @Path("/approval")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "审批")
    public Response approval(@ApiParam(value = "param", required = true) ApprovalReq approvalReq)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            approvalService.approval(approvalReq);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "approval", e, map);
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/queryApprovalLog")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询审批日志")
    public Response queryApprovalLog(@ApiParam(value = "param", required = true) ApprovalLogReq approvalLogReq)
    {
        CommonResultMap map = new CommonResultMap();
        
        try
        {
            List<ApprovalLogRecord> list = approvalService.queryApprovalLog(approvalLogReq);
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "approval", e, map);
        }
        
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
